Page({
  data: {
    searchWord: '',
  },
  onInputChange(e) {
    this.setData({
      searchWord: e.detail.value,
    });
  },
  searchHealthSkill() {
    const that = this;
    if (!that.data.searchWord.trim()) {
      wx.showToast({
        title: '请输入健康问题',
        icon: 'none',
      });
      return;
    }
    wx.request({
      url: 'https://apis.tianapi.com/healthskill/index',
      method: 'POST',
      data: {
        key: '3999c72f70747aa04ff700113680d4a0', // 替换成自己的APIKEY
        word: that.data.searchWord,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      success: function (res) {
        if (res.data.code == 200 && res.data.result.list && res.data.result.list.length > 0) {
          wx.navigateTo({
            url: `/pages/healthDetail/healthDetail?list=${encodeURIComponent(JSON.stringify(res.data.result.list))}`,
          });
        } else {
          wx.showToast({
            title: '暂无相关小妙招',
            icon: 'none',
          });
        }
      },
      fail: function (err) {
        console.log(err);
        wx.showToast({
          title: '网络错误',
          icon: 'none',
        });
      },
    });
  },
});